const LEJU_TOKEN = 'leju-token-admin'
const LEJU_USERINFO = 'leju-userinfo-admin'

/**
 * 保存token
 * @param {*} token  string
 */
export function setToken(token) {
  window.localStorage.setItem(LEJU_TOKEN, token)
}

/**
 *
 * @returns 返回token
 */
export function getToken() {
  const token = window.localStorage.getItem(LEJU_TOKEN)
  if (token) {
    return token
  } else {
    return null
  }
}

/**
 *
 */
export function removeToken() {
  window.localStorage.removeItem(LEJU_TOKEN)
}

export function saveUserInfo(userInfo) {
  try {
    window.localStorage.setItem(LEJU_USERINFO, JSON.stringify(userInfo))
  } catch (error) {
    console.log('保存用户信息异常error=>', error)
  }
}

export function getUserInfo() {
  const userinfoStr = window.localStorage.getItem(LEJU_USERINFO)
  const userInfoObj = JSON.parse(userinfoStr)
  if (userInfoObj) {
    return userInfoObj
  } else {
    return null
  }
}

export function removeUserInfo() {
  window.localStorage.removeItem(LEJU_USERINFO)
}

